/**
 * Created by abiu on 2016-03-03.
 */
var api = require('../model').api;


var countsPerPage = 7;
//获取接口列表
exports.getApis = function* () {

    var condition = {project_id: this.params.project_id};
    //if (this.request.isManager && 1 != this.session.user.user_power)
    //    condition['api_create_by']= this.session.user._id;
    var from = parseInt(this.params.from);
    var counts = parseInt(this.params.counts);
    var filter = {_id:1,api_name:1,api_type:1,api_url:1,api_description:1};

    try {
        var find = yield api.find(condition,filter).sort({api_url:1}).skip(from).limit(counts);
    }
    catch (err) {
        find = {err:err,status:500};
    }

    this.send(find);

}

//获取接口总数
exports.getApisCounts = function* () {
    var condition = {project_id: this.params.project_id};

    try {
        var find = yield api.count(condition);
    }
    catch (err) {
        find = {err:err,status:500};
    }

    this.send(find);
}


//获取接口详情
exports.getApi = function* () {

    var condition = {_id: this.params._id};

    try {
        var find = yield api.findOne(condition);
    }
    catch (err) {
        find = {err:err,status:500};
    }

    this.send(find);

}
//新增接口
exports.addApi = function*() {
    var data = this.request.body;
    var ob = {
        project_id: data.project_id,
        api_name: data.api_name,
        api_url: data.api_url,
        api_type: data.api_type,
        api_description: data.api_description,

    }
    try {
        var create = yield  api.create(ob);
    }
    catch (err) {
       create = {err:err,status:500};
    }

    this.send(create);

}

//更新接口
exports.updateApi = function*() {
    var data = this.request.body;

    try {
        var update = yield api.update({_id: data._id}, {$set: data});
    }
    catch (err) {
        update = {err:err,status:500}
    }

    this.send(update);


}


//删除接口
exports.deleteApi = function* () {
    var condition = {_id: this.request.body._id}
    var remove = "success";
    try {
         yield api.remove(condition);
    }
    catch (err) {
        remove = {err:err,status:500}
    }
    this.send(remove);

}

